package routers

import (
	"erp/controllers"

	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

func SetupRouter(r *gin.Engine, db *gorm.DB) {
	warehouseRouter := r.Group("/api/warehouses")
	{
		warehouseRouter.GET("/", adaptGetWarehouses(db))
		warehouseRouter.POST("/", adaptCreateWarehousesList(db))
		warehouseRouter.PUT("/", adaptUpdWarehouse(db))
		warehouseRouter.POST("/order", UpdateWarehouseOrder(db))
		warehouseRouter.DELETE("/:warehouseId", adaptDeleteWarehouses(db))
		//warehouseRouter.GET("/export", controllers.ExportWarehouses)
	}

	productRouter := r.Group("/api/products")
	{
		productRouter.GET("/", adaptGetProducts(db))
		productRouter.GET("/singleProducts/:productId", GetSingleProduct(db))
		//productRouter.POST("/upload", uploadHandlers(db))
		productRouter.POST("/", adaptCreateProduct(db))
		productRouter.PUT("/", adaptUpdProduct(db))
		productRouter.DELETE("/:productId", adaptDeleteProduct(db))
		productRouter.GET("/export-excel", productsExportExcel(db))
		productRouter.POST("/import-excel", ImportProductsFromExcel(db))
		//productRouter.GET("/export", controllers.ExportProducts)
	}

	inventoryRouter := r.Group("/api/inventory")
	{
		inventoryRouter.GET("/", adaptGetInventoryRecords(db))
		inventoryRouter.POST("/", adaptCreateInventoryRecord(db))
		inventoryRouter.DELETE("/:inventoryId", adaptDeleteInventoryId(db))
		//inventoryRouter.GET("/export", controllers.ExportInventoryRecords)
	}

	fileRouter := r.Group("/api/file")
	{
		fileRouter.POST("/upload", adaptFileUpload())
		fileRouter.GET("/download", adaptFileDownload())
	}

	// 添加系统设置API路由
	settingsRouter := r.Group("/api/settings")
	{
		settingsRouter.GET("/", adaptGetSystemSettings(db))
		settingsRouter.PUT("/", adaptUpdateSystemSetting(db))
	}

	// 添加密码管理API路由
	passwordRouter := r.Group("/api/password")
	{
		passwordRouter.GET("/check", adaptCheckPasswordExists(db))
		passwordRouter.POST("/set", adaptSetPassword(db))
		passwordRouter.POST("/change", adaptChangePassword(db))
		passwordRouter.POST("/verify", adaptVerifyPassword(db))
	}

	// 添加邮件配置API路由
	emailRouter := r.Group("/api/email")
	{
		emailRouter.GET("/config", adaptGetEmailConfig(db))
		emailRouter.PUT("/config", adaptUpdateEmailConfig(db))
		emailRouter.POST("/test", adaptTestEmail(db))
		emailRouter.GET("/overdue", adaptCheckOverdueExists(db))
	}

	r.GET("/", controllers.Index)
	r.GET("/inventory/:productId", controllers.Inventory)
}

func adaptGetSystemSettings(db *gorm.DB) gin.HandlerFunc {
	return func(c *gin.Context) {
		controllers.GetSystemSettings(c, db)
	}
}

func adaptUpdateSystemSetting(db *gorm.DB) gin.HandlerFunc {
	return func(c *gin.Context) {
		controllers.UpdateSystemSetting(c, db)
	}
}

func adaptFileDownload() gin.HandlerFunc {
	return func(c *gin.Context) {
		controllers.DownloadFile(c)
	}
}

func adaptFileUpload() gin.HandlerFunc {
	return func(c *gin.Context) {
		controllers.UploadFile(c)
	}
}

func adaptUpdProduct(db *gorm.DB) gin.HandlerFunc {
	return func(c *gin.Context) {
		controllers.UpdProduct(c, db)
	}
}

func adaptUpdWarehouse(db *gorm.DB) gin.HandlerFunc {
	return func(c *gin.Context) {
		controllers.UpdWarehouse(c, db)
	}
}
func UpdateWarehouseOrder(db *gorm.DB) gin.HandlerFunc {
	return func(c *gin.Context) {
		controllers.UpdateWarehouseOrder(c, db)
	}
}

func adaptDeleteInventoryId(db *gorm.DB) gin.HandlerFunc {
	return func(c *gin.Context) {
		controllers.DeleteInventory(c, db)
	}
}

func adaptCreateInventoryRecord(db *gorm.DB) gin.HandlerFunc {
	return func(c *gin.Context) {
		controllers.CreateInventoryRecord(c, db)
	}
}

func adaptDeleteProduct(db *gorm.DB) gin.HandlerFunc {
	return func(c *gin.Context) {
		controllers.DeleteProduct(c, db)
	}
}

func adaptCreateProduct(db *gorm.DB) gin.HandlerFunc {
	return func(c *gin.Context) {
		controllers.CreateProduct(c, db)
	}
}

func adaptGetWarehouses(db *gorm.DB) gin.HandlerFunc {
	return func(c *gin.Context) {
		controllers.GetWarehouses(c, db)
	}
}
func adaptCreateWarehousesList(db *gorm.DB) gin.HandlerFunc {
	return func(c *gin.Context) {
		controllers.CreateWarehouse(c, db)
	}
}

func adaptGetProducts(db *gorm.DB) gin.HandlerFunc {
	return func(c *gin.Context) {
		controllers.GetProducts(c, db)
	}
}

func GetSingleProduct(db *gorm.DB) gin.HandlerFunc {
	return func(c *gin.Context) {
		controllers.GetSingleProduct(c, db)
	}
}

//func uploadHandlers(db *gorm.DB) gin.HandlerFunc {
//	return func(c *gin.Context) {
//		controllers.UploadHandler(c, db)
//	}
//}

func adaptGetInventoryRecords(db *gorm.DB) gin.HandlerFunc {
	return func(c *gin.Context) {
		controllers.GetInventoryRecords(c, db)
	}
}

func adaptDeleteWarehouses(db *gorm.DB) gin.HandlerFunc {
	return func(c *gin.Context) {
		controllers.DeleteWarehouse(c, db)
	}
}

func productsExportExcel(db *gorm.DB) gin.HandlerFunc {
	return func(c *gin.Context) {
		controllers.ExportProductsToExcel(c, db)
	}
}

func ImportProductsFromExcel(db *gorm.DB) gin.HandlerFunc {
	return func(c *gin.Context) {
		controllers.ImportProductsFromExcel(c, db)
	}
}

// 密码管理相关适配器函数
func adaptCheckPasswordExists(db *gorm.DB) gin.HandlerFunc {
	return func(c *gin.Context) {
		controllers.CheckPasswordExists(c, db)
	}
}

func adaptSetPassword(db *gorm.DB) gin.HandlerFunc {
	return func(c *gin.Context) {
		controllers.SetPassword(c, db)
	}
}

func adaptChangePassword(db *gorm.DB) gin.HandlerFunc {
	return func(c *gin.Context) {
		controllers.ChangePassword(c, db)
	}
}

func adaptVerifyPassword(db *gorm.DB) gin.HandlerFunc {
	return func(c *gin.Context) {
		controllers.VerifyPassword(c, db)
	}
}

// 邮件配置相关适配器函数
func adaptGetEmailConfig(db *gorm.DB) gin.HandlerFunc {
	return func(c *gin.Context) {
		controllers.GetEmailConfig(c, db)
	}
}

func adaptUpdateEmailConfig(db *gorm.DB) gin.HandlerFunc {
	return func(c *gin.Context) {
		controllers.UpdateEmailConfig(c, db)
	}
}

func adaptTestEmail(db *gorm.DB) gin.HandlerFunc {
	return func(c *gin.Context) {
		controllers.TestEmail(c, db)
	}
}

func adaptCheckOverdueExists(db *gorm.DB) gin.HandlerFunc {
	return func(c *gin.Context) {
		controllers.CheckOverdueExists(c, db)
	}
}
